﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
/*reference HyperLinkField*/
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Water.Web.Controls.BaseInterface;
using System.Globalization;
using System.ComponentModel;
using System.Drawing.Design;
using Water.Resources.Globalize;

namespace Water.Web.Controls
{
    public class NestedField : DataControlField, IDataControlFieldPrompt
    {
        private PropertyDescriptor[] urlFieldDescs;
        private string headerPrpmpt;
        private string footerPrompt;
        private string title = "sys.expand_collapse";
        private NestedType nestedType = NestedType.Iframe;
        private Unit height = Unit.Empty;

        #region IDataControlFieldPrompt 成员

        [Browsable(true), GlobalizeCategory("PROMPT"), GlobalizeDescription("HeaderPrpmpt")]
        public virtual string HeaderPrompt
        {
            get { return headerPrpmpt; }
            set { headerPrpmpt = value; }
        }

        [Browsable(true), GlobalizeCategory("PROMPT"), GlobalizeDescription("FooterPrompt")]
        public virtual string FooterPrompt
        {
            get { return footerPrompt; }
            set { footerPrompt = value; }
        }

        #endregion

        public override string HeaderText
        {
            get
            {
                if (string.IsNullOrEmpty(base.HeaderText) && !string.IsNullOrEmpty(HeaderPrompt))
                    base.HeaderText = Water.Web.Prompt.PromptProxy.GetPromptDesc(HeaderPrompt);
                return base.HeaderText;
            }
            set
            {
                base.HeaderText = value;
            }
        }

        public override string FooterText
        {
            get
            {
                if (string.IsNullOrEmpty(base.FooterText) && !string.IsNullOrEmpty(FooterPrompt))
                    base.FooterText = Water.Web.Prompt.PromptProxy.GetPromptDesc(FooterPrompt);
                return base.FooterText;
            }
            set
            {
                base.FooterText = value;
            }
        }

        [GlobalizeDescription("DataNavigateUrlFields"), Category("Data"), DefaultValue((string)null), Editor("System.Web.UI.Design.WebControls.DataFieldEditor, System.Design, Version=2.0.0.0, Culture=neutral", typeof(UITypeEditor)), TypeConverter(typeof(StringArrayConverter))]
        public virtual string[] DataNavigateUrlFields
        {
            get
            {
                object obj2 = base.ViewState["DataNavigateUrlFields"];
                if (obj2 != null)
                {
                    return (string[])((string[])obj2).Clone();
                }
                return new string[0];
            }
            set
            {
                string[] strArray = base.ViewState["DataNavigateUrlFields"] as string[];
                if (!this.StringArraysEqual(strArray, value))
                {
                    if (value != null)
                    {
                        base.ViewState["DataNavigateUrlFields"] = (string[])value.Clone();
                    }
                    else
                    {
                        base.ViewState["DataNavigateUrlFields"] = null;
                    }
                    this.OnFieldChanged();
                }
            }
        }

        [GlobalizeDescription("DataNavigateUrlFormatString"), Category("Data"), DefaultValue("")]
        public virtual string DataNavigateUrlFormatString
        {
            get
            {
                object obj2 = base.ViewState["DataNavigateUrlFormatString"];
                if (obj2 != null)
                {
                    return (string)obj2;
                }
                return string.Empty;
            }
            set
            {
                if (!object.Equals(value, base.ViewState["DataNavigateUrlFormatString"]))
                {
                    base.ViewState["DataNavigateUrlFormatString"] = value;
                    this.OnFieldChanged();
                }
            }
        }

        [Category("Layout"), DefaultValue(typeof(Unit), ""), GlobalizeDescription("Height")]
        public virtual Unit Height
        {
            get
            {
                return this.height;
            }
            set
            {
                this.height = value;
            }
        }

        [Browsable(true), Category("Appearance"), GlobalizeDescription("Title"), Bindable(true), DefaultValue("sys.expand_collapse")]
        public string Title
        {
            get
            {
                if (!string.IsNullOrEmpty(this.title))
                    return Water.Web.Prompt.PromptProxy.GetPromptDesc(this.title);
                return title;
            }
            set { title = value; }
        }

        [Category("Layout"), DefaultValue(NestedType.Iframe), GlobalizeDescription("NestedType")]
        public NestedType NestedType
        {
            get { return nestedType; }
            set { nestedType = value; }
        }

        public NestedField()
        {
            this.HeaderStyle.Width = Unit.Pixel(21);
        }

        protected override DataControlField CreateField()
        {
            return new NestedField();
        }

        public override void InitializeCell(System.Web.UI.WebControls.DataControlFieldCell cell, System.Web.UI.WebControls.DataControlCellType cellType, System.Web.UI.WebControls.DataControlRowState rowState, int rowIndex)
        {
            base.InitializeCell(cell, cellType, rowState, rowIndex);

            if (cellType == DataControlCellType.DataCell)
            {
                HtmlGenericControl div = new HtmlGenericControl("div");
                div.ID = "nested";
                div.Attributes.Add("class", "default_nested_icon default_nested_plus");
                div.Attributes.Add("title",this.Title);


                if (((rowState & DataControlRowState.Insert) == DataControlRowState.Normal) && base.Visible)
                {
                    if ((this.DataNavigateUrlFields.Length != 0))
                    {
                        div.DataBinding += new EventHandler(this.OnDataBindField);
                    }
                    cell.Controls.Add(div);
                }

            }
        }

        private void OnDataBindField(object sender, EventArgs e)
        {
            HtmlGenericControl div = (HtmlGenericControl)sender;
            Control namingContainer = div.NamingContainer;
            object component = null;
            if (namingContainer == null)
            {
                throw new HttpException(string.Format(ExceptionResource.DataControlField_NoContainer));
            }
            component = DataBinder.GetDataItem(namingContainer);
            if ((component == null) && !base.DesignMode)
            {
                throw new HttpException(string.Format(ExceptionResource.DataItem_Not_Found));
            }
            if (this.urlFieldDescs == null)
            {
                PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(component);

                string dataTextField;//= this.DataTextField;

                string[] dataNavigateUrlFields = this.DataNavigateUrlFields;
                int num = dataNavigateUrlFields.Length;
                this.urlFieldDescs = new PropertyDescriptor[num];
                for (int i = 0; i < num; i++)
                {
                    dataTextField = dataNavigateUrlFields[i];
                    if (dataTextField.Length != 0)
                    {
                        this.urlFieldDescs[i] = properties.Find(dataTextField, true);
                        if ((this.urlFieldDescs[i] == null) && !base.DesignMode)
                        {
                            throw new HttpException(string.Format(ExceptionResource.DataNavigateUrlField_Not_Found, new object[] { dataTextField }));
                        }
                    }
                }
            }
         
            int length = this.urlFieldDescs.Length;
            string str3 = string.Empty;
            if (((this.urlFieldDescs != null) && (length > 0)) && (component != null))
            {
                object[] dataUrlValues = new object[length];
                for (int j = 0; j < length; j++)
                {
                    if (this.urlFieldDescs[j] != null)
                    {
                        dataUrlValues[j] = this.urlFieldDescs[j].GetValue(component);
                    }
                }
                string s = this.FormatDataNavigateUrlValue(dataUrlValues);
                str3 = s;
                /*
                if (!CrossSiteScriptingValidation.IsDangerousUrl(s))
                {
                    str3 = s;
                }
                */
            }
            if ((base.DesignMode && (this.DataNavigateUrlFields.Length != 0)) && (str3.Length == 0))
            {
                str3 = "url";
            }
            if (str3.Length > 0)
            {
                string js = "$j.nested(this,\'"
                    + str3
                    + "\',\'"
                    + this.NestedType.ToString().ToLower()
                    + "\'";
                if (this.Height != Unit.Empty)
                    js += "," + this.Height.Value;
                js += ")";
                div.Attributes.Add("onclick", js);
            }

        }

        protected virtual string FormatDataNavigateUrlValue(object[] dataUrlValues)
        {
            string str = string.Empty;
            if (dataUrlValues == null)
            {
                return str;
            }
            string dataNavigateUrlFormatString = this.DataNavigateUrlFormatString;
            if (dataNavigateUrlFormatString.Length == 0)
            {
                if ((dataUrlValues.Length > 0) && !this.IsNull(dataUrlValues[0]))
                {
                    str = dataUrlValues[0].ToString();
                }
                return str;
            }
            return string.Format(CultureInfo.CurrentCulture, dataNavigateUrlFormatString, dataUrlValues);
        }

        private bool IsNull(object value)
        {
            if ((value != null) && !Convert.IsDBNull(value))
            {
                return false;
            }
            return true;
        }

        private bool StringArraysEqual(string[] arr1, string[] arr2)
        {
            if ((arr1 != null) || (arr2 != null))
            {
                if ((arr1 == null) || (arr2 == null))
                {
                    return false;
                }
                if (arr1.Length != arr2.Length)
                {
                    return false;
                }
                for (int i = 0; i < arr1.Length; i++)
                {
                    if (!string.Equals(arr1[i], arr2[i]))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

    }

    public enum NestedType
    {
        Iframe,
        Ajax,
        Popup,
        NewWindow
    }
}
